from odoo import fields, models, api
from odoo.exceptions import UserError

TRANSLATION_LEVEL = [('fir', '一级'), ('sec', '二级'), ('trd', '三级')]
from ...functions.rp_translation_index_fc import RPTranslationIndexFC


class RPTranslationIndex(models.Model):
    _name = 'rp.translation.index'
    _description = "翻译索引"
    _rec_name = "code"
    _order = 'code'
    _sql_constraints = [('unique_name', 'UNIQUE(code)', '编码不能重复')]

    code = fields.Char(string='代码', index=True, copy=False, compute='_depends_joint_code', store=True)

    code_fir = fields.Char(string='一级代码')
    code_sec = fields.Char(string='二级代码')
    code_trd = fields.Char(string='三级代码')

    desc = fields.Text(string='描述')

    level = fields.Selection(string='级别', selection=TRANSLATION_LEVEL, default=TRANSLATION_LEVEL[0][0], compute='_depends_fulfill_level', store=True)
    parent_id = fields.Many2one(string='上级', comodel_name='rp.translation.index', compute='_dependent_fulfill_parent', store=True)

    active = fields.Boolean(string='有效', default=True)

    @api.depends('code_fir', 'code_sec', 'code_trd')
    def _depends_joint_code(self):
        """ 拼接编码 """
        for s in self:
            code = ''
            if s.code_fir:
                code = s.code_fir
                if s.code_sec:
                    code = s.code_fir + '.' + s.code_sec
                    if s.code_trd:
                        code = s.code_fir + '.' + s.code_sec + '.' + s.code_trd
            s.write({'code': code})

    @api.depends('code_fir', 'code_sec', 'code_trd')
    def _depends_fulfill_level(self):
        """ 填充级别 """
        for s in self:
            level = TRANSLATION_LEVEL[0][0]
            if s.code_fir:
                level = TRANSLATION_LEVEL[0][0]
                if s.code_sec:
                    level = TRANSLATION_LEVEL[1][0]
                    if s.code_trd:
                        level = TRANSLATION_LEVEL[2][0]
            s.write({'level': level})

    @api.depends('level')
    def _dependent_fulfill_parent(self):
        """ 填充上级 """
        for s in self:
            parent = self.browse()
            if s.level == TRANSLATION_LEVEL[2][0]:
                parent = self.search([('level', '=', TRANSLATION_LEVEL[1][0]), ('code_fir', '=', s.code_fir), ('code_sec', '=', s.code_sec)])
            elif s.level == TRANSLATION_LEVEL[1][0]:
                parent = self.search([('level', '=', TRANSLATION_LEVEL[0][0]), ('code_fir', '=', s.code_fir)])
            s.write({'parent_id': parent})

    @api.model_create_multi
    def create(self, vals_list):
        results = super(RPTranslationIndex, self).create(vals_list)
        for result in results:
            print(result)
            # 调用功能函数，触发创建上级
            upper_code = RPTranslationIndexFC(self.env).get_upper_code(result.code)
            if upper_code:
                RPTranslationIndexFC(self.env).get_index(upper_code)
        return results

    def write(self, vals):
        code = vals.get('code',False)
        if code:
            upper_code = RPTranslationIndexFC(self.env).get_upper_code(code)
            if upper_code:
                RPTranslationIndexFC(self.env).get_index(upper_code)
        return super(RPTranslationIndex, self).write(vals)

    def name_get(self):
        res = []
        for s in self:
            name = s._get_name()
            res.append((s.id, name))
        return res

    def _get_name(self):
        name = self.code or ''
        if self._context.get('show_desc'):
            desc = self.desc if self.desc else '\n'

            name = name + "\n" + desc
            name = name.replace('\n\n', '\n')

        return name
